export default {
	async productGet() {
		let res  = await this.httptool.post(this.apis.productGet, {id: this.id});
		this.modelData = res.data;
		this.buyParams.qty = this.modelData.minBuyQty;
		this.productPrice();
	},
	async collection() {
		let collect = this.modelData.collectFlag;		
		let res = await this.httptool.post(this.apis.productCollect, {
			id: this.modelData.id
		});
		console.log(res);
		this.modelData.collectFlag = !this.modelData.collectFlag;
		let str = this.modelData.collectFlag ? '收藏成功!' : '取消收藏成功!';
		this.$message.success(str);
		
		
	},
	async shopcarAddProduct() {
		//检查库存
		let res_ = await this.checkProductQty();
		//加入购物车
		let res = await this.httptool.post(this.apis.shopcarAdd, this.buyParams);
		this.$message.success('添加成功!');
		
		if(this.buyParams.buyNow == 1) {//如果是立即购买，判断是否有默认地址，有就跳转到确认订单页面	
			await this.hasPermission(1);		
			let res_ = await this.httptool.post(this.apis.getDefaultAddress, null);
			let address = res_.data;
			this.$store.commit('changeValue', {
				name: 'confrimorder',
				value: {
					ids: res.data.id,
					address: address
				}
			});
			if(address && address.id) {
				this.$router.push('/orderconfirm');
			} else {
				this.$confirm('您还没有收货地址，赶紧去添加吧？').then(() => {
					this.$router.push('/address?type=order');
				});
			}
		}
		this.$refs.priceHeader.shopcarAPICount();
	},
	async checkProductQty() {
		let checkParam = {};
		checkParam.list = [];
		checkParam.list.push({
			productId: this.buyParams.productId,
			qty: this.buyParams.qty
		});
		return this.httptool.post(this.apis.checkQty, checkParam);
	},

	async productPrice() {
		let arr = ['', '7', '30', '90']
		let params = {
			name: this.modelData.name,
			time: arr[this.timeType],
			id:this.modelData.id			
		}
		if(this.modelData.categoryId=='5'){
			params.deviceFlag=true;
		}
		let res = await this.httptool.post(this.apis.productPrice, params);
		let data = res.data;
		this.xAxis = data.price.dates;
		this.yAxis = data.price.prices;
		this.lowest = data.lowest;
		this.$nextTick(()=>{
			this.drawLine();
		})
	}
}